package com.zk.iotgateway.service.impl;

import com.zk.iotgateway.manager.GlobalManager;
import com.zk.iotgateway.dao.IotDispatchMapper;
import com.zk.iotgateway.domain.param.IotDispatchDO;
import com.zk.iotgateway.rabbitmq.PublisherConfig;
import com.zk.iotgateway.service.IotDispatchService;
import io.netty.channel.ChannelHandlerContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ${DESCRIPTION}
 *
 * @author:mn.m
 * @create 2019-05-05 下午 04:22
 **/
@Service
@Transactional
public class IotDispatchServiceImpl implements IotDispatchService {

   private  Map<String, PublisherConfig> configMap =null;



    @Resource
    private IotDispatchMapper iotDispatchMapper;

    @Override
    public List<Map<String, String>> findDispatchList() {
        return iotDispatchMapper.listDispatch();
    }

    @Override
    public IotDispatchDO findById(String id) {
        return iotDispatchMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean save(IotDispatchDO iotDispatchDO) {
        iotDispatchMapper.insertSelective(iotDispatchDO);
        return true;
    }

    @Override
    public boolean remove(String id) {
        iotDispatchMapper.deleteByPrimaryKey(id);
        return true;
    }

    @Override
    public boolean removeByPort(String port) {
        iotDispatchMapper.deleteByPort(port);
        return true;
    }

    @Override
    public boolean updateEntity(IotDispatchDO t) {
        iotDispatchMapper.updateByPrimaryKeySelective(t);
        return true;
    }

    @Override
    public List<IotDispatchDO> findByCondition(String k) {
        return iotDispatchMapper.listByPort(k);
    }

   /**
    * @Author mn
    * @Description
    * @Date 下午 03:38 2019/5/8 0008
    * @Param k
    * @return list
    **/
    @Override
    public List<IotDispatchDO> findValidByPort(String k) {
        return iotDispatchMapper.listvalidByPort(k);
    }


    @Override
    public void removeAllMq(String port) {
        if (!StringUtils.isEmpty(port)){
            GlobalManager.MQMANAGER.remove(port);
        }
    }

    @Override
    public void removeMQ(IotDispatchDO iotDispatchDO) {
        if (iotDispatchDO!=null &&!StringUtils.isEmpty(iotDispatchDO.getPort())){
            configMap = GlobalManager.MQMANAGER.get(iotDispatchDO.getPort());
            if (configMap !=null){
                System.out.println("bioc---"+ GlobalManager.MQMANAGER.size());
                System.out.println("bmqsize---"+configMap.size());
                configMap.remove(iotDispatchDO.getId());
                System.out.println("aioc---"+ GlobalManager.MQMANAGER.size());
                System.out.println("amqsize---"+configMap.size());
            }
        }
    }

    @Override
    public void startAllMq(List<IotDispatchDO> list) {
        if (list!=null&&!list.isEmpty()){
            for (IotDispatchDO item:list) {
                this.addMq(item);
            }
        }
    }

    @Override
    public void addMq(IotDispatchDO iotDispatchDO) {
        PublisherConfig publisherConfig= null;
        if (iotDispatchDO!=null &&!StringUtils.isEmpty(iotDispatchDO.getPort())){
            configMap= GlobalManager.MQMANAGER.get(iotDispatchDO.getPort());
            if(configMap==null){
               configMap =new HashMap<>();
            }
            System.out.println("bioc---"+ GlobalManager.MQMANAGER.size());
            System.out.println("bmqsize---"+configMap.size());
            publisherConfig = new PublisherConfig();
            publisherConfig.initConfig(iotDispatchDO);
            configMap.put(iotDispatchDO.getId(),publisherConfig);
            GlobalManager.MQMANAGER.put(iotDispatchDO.getPort(),configMap);
            System.out.println("aioc---"+ GlobalManager.MQMANAGER.size());
            System.out.println("amqsize---"+configMap.size());
        }
    }

    @Override
    public void send(ChannelHandlerContext ctx, String info) {
        PublisherConfig publisherConfig =null;
        String tmp=ctx.channel().localAddress()+"";
        String iotPort=tmp.substring(tmp.lastIndexOf(":")+1);
        configMap= GlobalManager.MQMANAGER.get(iotPort);
        if (configMap!=null&&configMap.size()>0){
            List<IotDispatchDO> list = iotDispatchMapper.listvalidByPort(iotPort);
            for (IotDispatchDO item:list){
                publisherConfig = configMap.get(item.getId());
                publisherConfig.send(item.getExchange(),item.getRoutingKey(),info);
            }
        }
    }
}
